package com.usb.serial.device;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.usb.serial.device.javaBean.InfoBean;

import java.util.List;
import java.util.Random;

public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.ButtonViewHolder> {
//    private List<String> dataList;
    private List<InfoBean.StatusBean> list;

    private OnItemClickListener mListener;
    public ButtonAdapter(List<InfoBean.StatusBean> list, OnItemClickListener listener) {
        this.list = list;
        this.mListener = listener;
    }

    @Override
    public ButtonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_button, null, false);
        return new ButtonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ButtonViewHolder holder, int position) {
        String buttonText = list.get(position).getName();
        holder.button.setText(buttonText);
        holder.button.setBackgroundColor(getRandomColor());
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在这里处理 item 点击事件
//                int positions = position; // 获取点击的位置
                if (position != RecyclerView.NO_POSITION && mListener != null) {
                    mListener.onItemClick(position);
                }
            }
        });
    }

    private int getRandomColor() {
        Random random = new Random();
        return Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ButtonViewHolder extends RecyclerView.ViewHolder {
        Button button;

        public ButtonViewHolder(View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
        }


    }

    // 定义接口用于回调点击事件
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}